/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 13, 2012  10:09:35 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.entitys.common
 * File Name : CommonService.java
 * TODO
 */
package promis.utility;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;

/**
 * @author chatree
 *
 */
public class CommonService {
	
	private static final Logger log = Logger.getLogger(CommonService.class);
	
	public static String CutString(String values, int length){
		if(!IsNull(values)){
			
			if(values.length() <= length)
				return values;
			else
				return values.substring(0,length);			
		}
		return values;
	}
	
	public static boolean IsNull(List lst){
		if(lst == null) 
			return true;
		else if(lst.size() <= 0)
			return true;
		else
			return false;
	}
	
	public static boolean IsNull(String value){
		if(value == null) 
			return true;
		else if(value.isEmpty())
			return true;
		else if(value.equals(""))
			return true;
		else
			return false;
	}
	
	public static int getSize(List lst){
		if(lst == null) 
			return 0;
		else
			return lst.size();
	}
	
	public static String IsNullRNull(String value){
		if(value == null) 
			return null;
		else if(value.isEmpty())
			return null;
		else if(value.equals(""))
			return null;
		else
			return value;
	}
	
	public static String ReplaceStringToTxtDatabase(StringBuffer value){
		if(!IsNull(value))
			return ReplaceStringToTxtDatabase(value.toString());
		return "";
	}
	
	public static String ReplaceStringToTxtDatabase(StringBuilder value){
		if(!IsNull(value))
			return ReplaceStringToTxtDatabase(value.toString());
		return "";
	}
	
	public static String ReplaceStringToTxtDatabase(String value){
		if(value == null) 
			return null;
		else if(value.isEmpty())
			return null;
		else if(value.equals(""))
			return null;
		else
			return value.replace("\'", "#|*|#");
	}
	
	public static String IsNullString(BigDecimal value){
		if(value == null) 
			return "";	
		else if(value.toString().equals(""))
			return "";
		else
			return value.toString();
	}
	
	public static String IsNullRNull(Object valueobj){
		
		if(valueobj == null) 
			return null;
		
		String value = valueobj.toString();
		if(value.isEmpty())
			return null;
		else if(value.equals(""))
			return null;
		else
			return value;
	}
	
	public static String IsNullRZero(String value){
		if(value == null) 
			return "0";
		else if(value.isEmpty())
			return "0";
		else if(value.equals(""))
			return "0";
		else
			return value;
	}
	
	public static String IsNullRZero(Integer value){
		if(value == null) 
			return "0";		
		else
			return value.intValue()+"";
	}
	
	public static String IsNullRZero(BigDecimal value){
		if(value == null) 
			return "0";
		else if(value.equals(""))
			return "0";
		else
			return value.toString();
	}
	
	public static String IsNullRString(String value){
		if(value == null) 
			return "";
		else if(value.isEmpty())
			return "";
		else if(value.equals(""))
			return "";
		else
			return value;
	}
	
	public static String IsNullRString(Object valueobj){
		
		if(valueobj == null) 
			return "";
		
		String value  = valueobj.toString();
		if(value.isEmpty())
			return "";
		else if(value.equals(""))
			return "";
		else
			return value;
		
	}
	
	public static boolean IsNull(Integer value){
		if(value == null) 
			return true;		
		else
			return false;
	}
	
	public static boolean IsNull(Double value){
		if(value == null) 
			return true;		
		else
			return false;
	}
	
	public static boolean IsNull(BigDecimal value){
		if(value == null) 
			return true;		
		else
			return false;
	}
	
	public static boolean IsNull(Object value){
		if(value == null) 
			return true;		
		else
			return false;
	}
	
	public static boolean IsNull(Long value){		
		if(value == null) 
			return true;		
		else
			return false;
	}
	
	public static int ConvertIntegerToIntValue(Integer value)  throws Exception {
		if(value == null){
			return 0;
		}else
			return value.intValue();
	}
	
	public static BigDecimal ConvertStringToBigDecimal(String value)  throws Exception {
		if(!CommonService.IsNull(value)){
			return new BigDecimal(CommonService.IsNullRZero(value));
		}else
			return new BigDecimal("0");
	}
	
	public static BigDecimal ConvertStringToBigDecimal(Object value)  throws Exception {
		if(!CommonService.IsNull(value)){
			return new BigDecimal(CommonService.IsNullRZero(value.toString()));
		}else
			return new BigDecimal("0");
	}
	
	public static long ConvertStringToLong(Object value)  throws Exception {
		if(!CommonService.IsNull(value)){
			return Long.parseLong(CommonService.IsNullRZero(value.toString()));
		}else
			return Long.parseLong("0");
	}
	
	public static long ConvertStringToLong(String value)  throws Exception {
		if(!CommonService.IsNull(value)){
			return Long.parseLong(CommonService.IsNullRZero(value));
		}else
			return Long.parseLong("0");
	}
	
	public static double ConvertStringToDouble(String value)  throws Exception {
		return Double.parseDouble(CommonService.IsNullRZero(value));
	}
	
	public static double ConvertBigDecimalToDouble(BigDecimal value)  throws Exception {
		return Double.parseDouble(CommonService.IsNullRZero(value));
	}
	
	/**
	 * 
	 * @param pms_staging_datetime Example : [YYYY-MM-DD//H24:MI:SS] 2012-03-01//01:01:01
	 * @return String Date Format  [YYYY-MM-DD] 2012-03-01
	 * @throws Exception
	 */
	public static String ConvertPmsStagingDateTime(String pms_staging_datetime) throws Exception
	{
		pms_staging_datetime = pms_staging_datetime.replace(" ", "");
		String [] temp = pms_staging_datetime.split("\\//");
		//String [] date = temp[0].split("\\-");
		//String [] time = temp[1].split("\\:");
				
		
		return temp[0];
	}
	
	
	/**
	 * 
	 * @param strDate Format : YYYY-MM-DD H24:MI:SS (exm : [2011-11-24 18:00:00])
	 * @return
	 */
	public static Date ConvertStringToDateTime(String strDate){
//		strDate = strDate.replace(" ", "");
//		String [] temp = strDate.split("\\|");
//		String [] date = temp[0].split("\\-");
//		String [] time = temp[1].split("\\:");
//		
//		System.out.println(temp[0]); //Date
//		System.out.println(temp[1]); //Time
//		
//		System.out.println(date[0]); //YYYY
//		System.out.println(date[1]); //MM		
//		System.out.println(date[2]); //DD
//		int day = Integer.parseInt(date[2]);
//		int month = Integer.parseInt(date[1]);
//		int year = Integer.parseInt(date[0]);
//		
//		System.out.println(time[0]); //H24
//		System.out.println(time[1]); //MI
//		System.out.println(time[2]); //SS
//		
//		int H24 = Integer.parseInt(time[0]);
//		int Mi = Integer.parseInt(time[1]);
//		int Sec = Integer.parseInt(time[2]);
		
		if(CommonService.IsNull(strDate)) return null;
		
		DateFormat formatter; 
		Date date = null;
		formatter = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE24TIME_STAGING);
		try {
			  
			date = (Date)formatter.parse(strDate);
			
		} catch (ParseException e) {
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		
		return date;
	}
	
	/**
	 * 
	 * @param date
	 * Return String Format : [yyyy-MM-dd HH:mm:ss]
	 */
	public static String ConvertDateTimeToString(Date date){
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE24TIME);
		String stringDate = "";
		try{
			
			stringDate = df.format(date);	        
	        
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}
	
	
	/**
	 * 
	 * @param date
	 * @return
	 * Return String Format : [dd-MM-yyyy HH:mm:ss]
	 */
	public static String ConvertDateTimeToStringDisplay(Date date){
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE24TIME_DISPLAY, Locale.US);
        
		String stringDate = "";
		try{
			
			stringDate = df.format(date);
	        
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}
	
	/**
	 * 
	 * @param date 
	 * @return String Format : [yyyy-MM-dd]
	 */
	public static String ConvertDateToString(Date date)
	{
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE_ONLY);
		String stringDate = "";
		try{
			
			stringDate = df.format(date);	        
	        
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}
	
	/**
	 * 
	 * @param date
	 * @return String Format : dd-MM-yyyy
	 */
	public static String ConvertDateToStringDisplay(Date date){
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE_ONLY_DISPLAY);
		String stringDate = "";
		try{
			
			stringDate = df.format(date);	        
	        
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}
	
	/**
	 * Format : MM-dd-yyyy
	 * @param date
	 * @return
	 */
	public static String ConvertDateToStringDisplay_MM_dd_YYYY(Date date){
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE_ONLY_DISPLAY_MM_dd_YYYY);
		String stringDate = "";
		try{
			
			stringDate = df.format(date);	        
	        
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}	
	
	/**
	 * format : MM/dd/yyyy
	 * @param date
	 * @return
	 */
	public static String ConvertDateToStringDisplay_MM__dd__YYYY(Date date){
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE_ONLY_DISPLAY_MM__dd__YYYY);
		String stringDate = "";
		try{
			
			stringDate = df.format(date);	        
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}	
	
	/**
	 * format : MM-dd-yyyy HH:mm:ss
	 * @param date
	 * @return
	 */
	public static String ConvertDateToStringDisplay_MM_dd_YYYY_Time(Date date){
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE_ONLY_DISPLAY_MM_dd_YYYY_Time);
		String stringDate = "";
		try{
			
			stringDate = df.format(date);	        
	        
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}	
	
	/**
	 * Format : MM/dd/yyyy HH:mm:ss
	 * @param date
	 * @return
	 */
	public static String ConvertDateToStringDisplay_MM__dd__YYYY_Time(Date date){
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE_ONLY_DISPLAY_MM__dd__YYYY_Time);
		String stringDate = "";
		try{
			
			stringDate = df.format(date);	        
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}	
	
	public static String ConvertDateToSAPStringDate(Date date){
		
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE_ONLY_SAP1);
		String stringDate = "";
		try{
			
			stringDate = df.format(date);	        
	        
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}
	
	
	public static String ConvertDateToSAPStringDateTime(Date date){
		
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE_ONLY_SAP2);
		String stringDate = "";
		try{
			
			stringDate = df.format(date);	        
	        
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}
	
	public static String ConvertDateToSAPStringDay(Date date){
		
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat.DATE_FORMAT_FULLDATE_ONLY_SAP_DAY);
		String stringDate = "";
		try{
			
			stringDate = df.format(date);	        
	        
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}
	
	public static String ConvertDateToSAPStringFormat(Date date, String CanlendarDateFormat){
		
		if(CommonService.IsNull(date)) return null;
		
		DateFormat df = new SimpleDateFormat(CanlendarDateFormat);
		String stringDate = "";
		try{
			
			stringDate = df.format(date);	        
	        
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return stringDate;
	}

	public static String ConvertConsStatusToString(String cons_status_int)
	{
		String temp = null;
		
		if(!CommonService.IsNull(cons_status_int)){
			if(cons_status_int.equals("O"))
				temp = "Work In Process";
			else if(cons_status_int.equals("A"))
				temp = "Completed";
			else if(cons_status_int.equals("C"))
				temp = "Closed";
			else if(cons_status_int.equals("I"))
				temp = "Inactive";
		}
		
		return temp;
	}
	
	public static String ConvertSaleStatusToString(String cons_status_int)
	{
		String temp = null;
		
		if(!CommonService.IsNull(cons_status_int)){
			if(cons_status_int.equals("O"))
				temp = "Work In Process";
			else if(cons_status_int.equals("A"))
				temp = "Completed";
			else if(cons_status_int.equals("C"))
				temp = "Closed";
				
		}
		
		return temp;
	}
	
	public static boolean findString(String stringVal, String stringFind)
	{
		if(!CommonService.IsNull(stringVal))
			return stringVal.contains(stringFind);
		else
			return false;
	}
	
	public static String ConvertPromisDocToDocumentNo(String document_type, String document_series, String document_number, String document_revision) throws Exception {
		if(CommonService.IsNull(document_type))
			throw new Exception("Error Require document_type is empty !!!");
		else if(CommonService.IsNull(document_series))
			throw new Exception("Error Require document_series is empty !!!");
		else if(CommonService.IsNull(document_number))
			throw new Exception("Error Require document_number is emty !!!");
		else if(ConvertStringToBigDecimal(document_number).intValue() <= 0)
			throw new Exception("Error Require document_number is emty !!!");
		
		String str = document_type;
		str += "-" + document_series;
		str += "-" + Utilitys.leftPad(document_number.toString(), 6, "0");
		
		if(!CommonService.IsNull(document_revision))
		{
			if(ConvertStringToBigDecimal(document_revision).intValue() >= 0)
				str += "-" + document_revision.toString();
		}
		
		return str;
	}
	
	public static String ConvertPromisDocToDocumentNo(String document_type, String document_series, BigDecimal document_number, BigDecimal document_revision) throws Exception {
		
		if(CommonService.IsNull(document_type))
			throw new Exception("Error Require document_type is empty !!!");
		else if(CommonService.IsNull(document_series))
			throw new Exception("Error Require document_series is empty !!!");
		else if(CommonService.IsNull(document_number))
			throw new Exception("Error Require document_number is emty !!!");
		else if(document_number.intValue() <= 0)
			throw new Exception("Error Require document_number is emty !!!");
		
		String str = document_type;
		str += "-" + document_series;
		str += "-" + Utilitys.leftPad(document_number.toString(), 6, "0");
		
		if(!CommonService.IsNull(document_revision))
		{
			if(document_revision.intValue() >= 0)
				str += "-" + document_revision.toString();
		}
		
		return str;
	}

	public static void main(String argsp[]){
		try{
			System.out.println(ReplaceStringToTxtDatabase("sdsd'sjsds,ds"));
		}catch(Exception e){
			
		}
	}

}
